package ilincar.systemcore.model;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.support.annotation.NonNull;

import ilincar.systemcore.service.CoreService;
import ilincar.systemcore.tool.ILog;
import ilincar.systemcore.util.SpUtil;

/**
 * Created by ilincar on 2016/12/9.
 */

public class CoreReceiver extends BroadcastReceiver {

    private final String TAG = "CoreReceiver";

    private CoreService mCoreService = null;
    private IntentFilter mIntentFilter = null;

    //汽车打火，熄火相关
    private final String CAR_ACTION = "carsignal";
    private final String CAR_MESSAGE = "carmode";
    private final String CAR_POWERON_WORKING = "car_poweron_working";       //汽车打火
    private final String CAR_POWERDOWN_SUSPEND = "car_powerdown_suspend";   //汽车熄火

    //汽车挂档相关
    private final String CAR_BACK_CAMERA_SIGNAL = "backcamsignal";
    private final String CAR_MSG_BACK_DOWN = "backcam_powerdown";   //汽车前进档
    private final String CAR_MSG_BACK_ON = "backcam_poweron";       //汽车后退档

    private final String ACTION_AKEY_ALARM_VIDEO = "com.ilincar.alarm.video";
    
    //网络连接相关
    private ConnectivityManager mConnectivityManager = null;

    public CoreReceiver(@NonNull CoreService service) {
        this.mCoreService = service;

        mConnectivityManager = (ConnectivityManager) mCoreService.getSystemService(Context.CONNECTIVITY_SERVICE);

        mIntentFilter= new IntentFilter();
        mIntentFilter.addAction(CAR_ACTION);    //获取汽车打火，熄火广播
        mIntentFilter.addAction(CAR_BACK_CAMERA_SIGNAL);    //获取汽车挂档广播
        mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); //WIFI状态改变广播
        mIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);   //网络状态变化
        
        mIntentFilter.addAction(ACTION_AKEY_ALARM_VIDEO);
    }

    public void registerReceiver(){
        mCoreService.registerReceiver(this, mIntentFilter);
    }


    public void unregisterReceiver(){
        mCoreService.unregisterReceiver(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String act = intent.getAction();
        if(CAR_ACTION.equals(act)){
            String msg = intent.getStringExtra(CAR_MESSAGE);
            if(msg.equals(CAR_POWERON_WORKING)){
                ILog.i("收到汽车打火广播......");
                mCoreService.start();
            }else if(msg.equals(CAR_POWERDOWN_SUSPEND)){
                ILog.i("收到汽车熄火广播......");
                mCoreService.stop();
            }
            
        }else if(CAR_BACK_CAMERA_SIGNAL.equals(act)){
            String msg = intent.getStringExtra(CAR_BACK_CAMERA_SIGNAL);
            if(msg.equals(CAR_MSG_BACK_DOWN)){
                ILog.d(TAG, "收到挂倒档广播.......");
            }else if(msg.equals(CAR_MSG_BACK_ON)){
                ILog.d(TAG, "收到挂前进档广播......");
            }
            
        }else if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(act)){
            int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            if(state == WifiManager.WIFI_STATE_ENABLED) {
                //已经打开了WIFI
                mCoreService.wifiOpened();
                SpUtil.setBooleanSharedPerference(context, "mIsWifiOpen", true);
            }
            
        }else if(ConnectivityManager.CONNECTIVITY_ACTION.equals(act)){
            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if(networkInfo != null){
                if(NetworkInfo.State.CONNECTED == networkInfo.getState() && networkInfo.isAvailable()){
                    //网络已经连接
                    //mCoreService.connectedNetwork();

                } else {
                    //网络断开

                }
            }
            
        }else if(ACTION_AKEY_ALARM_VIDEO.equals(act)){
            //请求一键报警视频
            mCoreService.onRequestUploadVideo(null);
            
        }
    }
}
